package com.boolib.fdfs.web;

import com.boolib.fdfs.ServerResponse;
import com.boolib.fdfs.WangEditorResponse;
import com.boolib.fdfs.util.FastDFSClient;
import com.boolib.fdfs.util.FileType;
import com.boolib.fdfs.util.ImageFileUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

@CrossOrigin
@RestController
public class WebController {

    String confUrl = new ClassPathResource("fastdfs-client.properties").getFile().getAbsolutePath();;

    public WebController() throws IOException {
    }

    @RequestMapping(value = "file/uploadFast", method = RequestMethod.POST)
    public ServerResponse uploadFast(@RequestParam("file") MultipartFile file) throws Exception {



        if(file.isEmpty() ){
            return ServerResponse.createByErrorMessage("ERROR：文件上传不能空");
        }






        // 1、把FastDFS提供的jar包添加到工程中
        // 2、初始化全局配置。加载一个配置文件。

        System.out.println(file.getOriginalFilename());
        System.out.println("lianjie");

        FastDFSClient fastDFSClient = new FastDFSClient(confUrl);

        String ext = FilenameUtils.getExtension(file.getOriginalFilename());


        String filePath = fastDFSClient.uploadFile(  file.getBytes(),ext);



        return ServerResponse.createBySuccess("http://www.2cryq.top/"+ filePath);

    }

    @RequestMapping(value = "/a", method = RequestMethod.GET)
    public String uploadFast1() throws Exception {




        System.out.println("lianjie2");

        return  ("返回路径：" +"http://www.2cryq.top/");

    }

    @RequestMapping(value = "file", method = RequestMethod.POST)
    public ServerResponse uploadFast2(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws Exception {

        Enumeration<String> e = request.getHeaders("Content-Type");


        if(file.isEmpty() ){
            return ServerResponse.createByErrorMessage("ERROR：文件上传不能空");
        }


        if(ImageFileUtil.isImage(file)){

            FastDFSClient fastDFSClient = new FastDFSClient(confUrl);

            String ext = FilenameUtils.getExtension(file.getOriginalFilename());


            String filePath = fastDFSClient.uploadFile(  file.getBytes(),ext);



            return ServerResponse.createBySuccess("http://www.2cryq.top/"+ filePath);

        }
        return  ServerResponse.createByErrorMessage("该文件不是Image类型");


    }



    @RequestMapping(value = "/editor/upload", method = RequestMethod.POST)
    public WangEditorResponse uploadFastEditor(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {


        if(file.isEmpty() ){
            return WangEditorResponse.createByError("ERROR：文件上传不能空");
        }


        if(ImageFileUtil.isImage(file)){

            FastDFSClient fastDFSClient = new FastDFSClient(confUrl);

            String ext = FilenameUtils.getExtension(file.getOriginalFilename());


            String filePath = fastDFSClient.uploadFile(  file.getBytes(),ext);



            return WangEditorResponse.createBySuccess("http://www.2cryq.top/"+ filePath);

        }
        return WangEditorResponse.createByError("该文件不是Image类型");


    }


    @PostMapping("/editor/save")
    public  ServerResponse save(String html){

        System.out.println(html);

        return ServerResponse.createBySuccess(html);
    }

}